﻿/*
Copyright 2009 Matías Fidemraizer (www.matiasfidemraizer.com).

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

using System;

using Comkarl;

namespace Comkarl.Architecture.Patterns.MVC.Entities
{
    /// <summary>
    /// Base type for an entity within a controller.
    /// </summary>
    /// <typeparam name="TIdentifierName">
    /// The type of the identifier's name.
    /// </typeparam>
    public abstract class ControlledEntity<TIdentifierName> : Entity<TIdentifierName> where TIdentifierName : IEquatable<TIdentifierName>
    {
        #region "Fields"
        /// <summary>
        /// Holds a reference to the controller.
        /// </summary>
        private Controller<TIdentifierName> associatedController;
        #endregion

        #region "Constructors"
        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="id">An identifier for the newly created entity instance.</param>
        public ControlledEntity(EntityIdentifier<TIdentifierName> id)
            : base(id)
        {
        }
        #endregion

        #region "Overridable methods"
        /// <summary>
        /// Associates the current entity to a provided controller.
        /// </summary>
        /// <param name="someController">
        /// The controller on which current entity is going to be associated.
        /// </param>
        public virtual void AssociateToController(Controller<TIdentifierName> someController)
        {
            // A controlled entity cannot be instantiated without providing a non-null associated controller.
            if (someController == null)
            {
                throw new NullControllerAssociationException<TIdentifierName>(
                    "NullControllerAssociation", 
                    this, 
                    new ArgumentNullException("someController")
                );
            }

            // A controlled entity cannot be associated to a controller from a different instance context.
            if (someController.AssociatedInstance != AssociatedInstance)
            {
                throw new InvalidControllerAssociationContextException<TIdentifierName>(
                    "InvalidControllerAssociationContext",
                    this,
                    new ArgumentException(MVCException<TIdentifierName>.GetLocalizedStringByID("InvalidControllerAssociationArgument"), "someController")
                );
            }

            this.associatedController = someController;
        }
        #endregion 

        #region "Properties"
        /// <summary>
        /// Gets a reference to the controller.
        /// </summary>
        public Controller<TIdentifierName> AssociatedController
        {
            get
            {
                return associatedController;
            }
        }
        #endregion
    }
}
